package org.wangtian.youxueban.excel.util;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.wangtian.youxueban.excel.bo.DaliyPracticeBo;

import java.io.File;
import java.io.FileInputStream;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ImportExcelUntil {

    /**
     *
     * @return
     */
    public List<DaliyPracticeBo> readXlsx(String dayName) {

        List<List<String>> result = new ArrayList<List<String>>();
        List<DaliyPracticeBo> daliyPracticeBos = new ArrayList<>();
        Workbook workbook = null;
        try {
            FileInputStream fis = null;
            File file = new File("C:\\data\\打卡数据\\健康打卡（正式）_"+dayName+".xlsx");
            fis = new FileInputStream(file);
            workbook = new XSSFWorkbook(fis);
            Sheet sheet = workbook.getSheetAt(0);

            int rowNum=sheet.getLastRowNum();
            System.out.printf(rowNum+"");
            for (int i = 1; i <rowNum ; i++) {
                Row row = sheet.getRow(i);
                if (row != null) {
                    DaliyPracticeBo daliyPracticeBo = new DaliyPracticeBo();
                    daliyPracticeBo.setCode(getString(row.getCell(0) ));
                    daliyPracticeBo.setMp3(getString(row.getCell(8) ));
                    daliyPracticeBo.setGroupName(getString(row.getCell(3) ));
                    daliyPracticeBo.setDay(DateUtil.toDate(row.getCell(4) +""));
                    daliyPracticeBo.setChenXing(getString(row.getCell(9) ));
                    daliyPracticeBo.setDuJing(getString(row.getCell(10) ));
                    daliyPracticeBo.setDaoGao(getString(row.getCell(11) ));
                    daliyPracticeBo.setFuYin(getString(row.getCell(12) ));
                    daliyPracticeBo.setMuYang(getString(row.getCell(13) ));
                    daliyPracticeBo.setShouJing(getString(row.getCell(14) ));
                    daliyPracticeBos.add(daliyPracticeBo);
                }
            }
            workbook.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return daliyPracticeBos;
    }



    /**
     *
     * @param cell
     * @return
     */
    private static String getString(Cell cell) {
        String val = "";
        try {
            val = String.valueOf(cell.getStringCellValue());
            if ("-".equals(val)){
                val="0";
            }
        } catch (IllegalStateException e) {
            val = String.valueOf(cell.getNumericCellValue());
        }

        if (val.indexOf("E") > -1) {//防止科学计数法  防止数字变成科学记数法
            DecimalFormat df = new DecimalFormat("0");
            val = df.format(cell.getNumericCellValue());
        }

        return val;
    }
}